import type {AjaxRequestConfig} from 'uni-ajax'
import { AjaxRequest } from '@/api'
import { useDark } from "@/composables";
// 系统信息
const systemInfo = uni.getWindowInfo()
// 胶囊位置
const capsuleInfo = uni.getMenuButtonBoundingClientRect()


export interface Response { type: 'success' | 'error', message: string, items: any[], total: number, lastPage: number, [index: string]: any, userInfo?: Record<string, any> }

export interface Options {
  config?: AjaxRequestConfig
  loading?: boolean | string
}

export declare type AppHooks = ReturnType<typeof useApp>

export function useApp() {
  const toast = useGlobalToast()
  const loading = useGlobalLoading()
  const message = useGlobalMessage()
  const api = new AjaxRequest(toast, loading)
  const isDark = useDark()
  const toggleDark = () => isDark.value = !isDark.value
  // 计算导航栏高度
  const navHeight = (systemInfo?.statusBarHeight||47) + capsuleInfo.height + (capsuleInfo.top - (systemInfo?.statusBarHeight||47)) * 2
  // 计算tabbar高度
  const tabHeight = systemInfo.safeArea.bottom - systemInfo.safeArea.height
  return reactive({
    toast,
    loading,
    message,
    api,
    navHeight,
    tabHeight,
    isDark,
    toggleDark
  })
}
